package com.netcracker.util.service;

import com.netcracker.db.exception.DBException;
import com.netcracker.entity.Publisher;
import com.netcracker.entity.record.PublisherRecord;
import com.netcracker.util.dao.DAOFacade;
import com.netcracker.util.service.exception.DataTransmittingException;
import com.netcracker.util.service.exception.NullDataException;
import com.netcracker.util.service.exception.WrongPublisherException;
import java.util.List;

public class PublisherService {
    
    private DAOFacade daoFacade;
    
    public PublisherService(DAOFacade daoFacade) {
        this.daoFacade = daoFacade;
    }
    
    public void setDAOFacade(DAOFacade daoFacade) {
        this.daoFacade = daoFacade;
    }
    
    public List<Publisher> findAllPublishers() throws DataTransmittingException, NullDataException {
        try {
            List<Publisher> publishers = daoFacade.getPublisherDAO().findAll();
            if (publishers == null) 
                throw new NullDataException("There are no publishers.");
            return publishers;
        } catch(DBException ex) {
            throw new DataTransmittingException(ex.getMessage(), ex);
        }
    }
    
    public Publisher findPublisher(int publisherID) throws DataTransmittingException, NullDataException {
        try {
            Publisher publisher = daoFacade.getPublisherDAO().find(publisherID);
            if (publisher == null) 
                throw new NullDataException("There are no publisher with this id.");
            return publisher;
        } catch(DBException ex) {
            throw new DataTransmittingException(ex.getMessage(), ex);
        }
    }
    
    public void deletePublisher(int publisherID) throws DataTransmittingException {
        try {
            daoFacade.getPublisherDAO().remove(publisherID);
        } catch(DBException ex) {
            throw new DataTransmittingException(ex.getMessage(), ex);
        }
    }
    
    public void addPublisher(PublisherRecord publisher) throws DataTransmittingException, 
            WrongPublisherException {
        try {
            checkRecord(publisher);
            daoFacade.getPublisherDAO().create(publisher);
        } catch(DBException ex) {
            throw new DataTransmittingException(ex.getMessage(), ex);
        }
    }
    
    public void updatePublisher(PublisherRecord publisher) throws DataTransmittingException, 
            WrongPublisherException {
        try {
            checkRecord(publisher);
            daoFacade.getPublisherDAO().edit(publisher);
        } catch(DBException ex) {
            throw new DataTransmittingException(ex.getMessage(), ex);
        }
    }

    private void checkRecord(PublisherRecord publisher) throws WrongPublisherException {
        if (publisher.getCountryID() == null || publisher.getName() == null)
            throw new WrongPublisherException("Not all fields of publisher are filled!");
    }
    
}
